package com.css.fxfzysx.modules.ysxManagement.repository.impl;

import com.css.fxfzysx.modules.ysxManagement.entity.HaManageEntity;
import com.css.fxfzysx.modules.ysxManagement.repository.HaManageRepository;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class HaManageRepositoryImpl implements HaManageRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void saveHaManage(HaManageEntity it) {
        jdbcTemplate.update("insert into czt_high_angle_manage(cham_id,structure_type,structure_value,create_user,create_time,del_flag,project_id,default_data) " +
                "values(?,?,?,?,?,?,?,?)", it.getChamId(), it.getStructureType(), it.getStructureValue(), it.getCreateUser(), it.getCreateTime(), it.getDelFlag(), it.getProjectId(),it.getDefaultData());
    }

    @Override
    public void updateHaManage(HaManageEntity it) {
        String sql = "update czt_high_angle_manage set structure_type= :structureType,structure_value= :structureValue,update_user= :updateUser,update_time= :updateTime,project_id= :projectId" +
                " where cham_id= '"+it.getChamId()+"'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(it);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public List<HaManageEntity> getAllHaManage() {
        String sql = "select * from czt_high_angle_manage where del_flag='0' order by create_time asc";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(HaManageEntity.class));
    }

    @Override
    public void deleteHaManageById(String haManageId) {
        String sql = "update czt_high_angle_manage set del_flag = '1' where cham_id=?";
        jdbcTemplate.update(sql, haManageId);
    }

    @Override
    public void deleteHaManageByProjectId(String arrangeId) {
        String sql = "update czt_high_angle_manage set del_flag = '1' where project_id=?";
        jdbcTemplate.update(sql, arrangeId);
    }

    @Override
    public List<HaManageEntity> getHaManageById(String chamId) {
        String sql = "select * from czt_high_angle_manage where cham_id = '"+chamId+"'";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(HaManageEntity.class));
    }
}
